import { Alova } from '@/utils/http/alova/index';

// 防溜作业信息数据结构
interface DefensiveTaskInfo {
  id: string;
  trainNumber: string; // 车次
  carriageNumber: number; // 车辆数
  enteringTime: string; // 进站时间
  departureTimeExpected?: string; // 预计离站时间
  direction: string; // 停车方向
  remark?: string; // 备注
  orgId: string;
  stationId: string;
  createTime?: string;
  updateTime?: string;
  groupAndTaskList?: DefensiveTaskDetail[]; // 编组和任务列表
}

// 防溜作业详细信息
interface DefensiveTaskDetail {
  id?: string;
  taskId?: string; // 关联的作业ID
  trackId: string; // 股道ID
  trackName?: string; // 股道名称
  groupId?: string; // 编组ID
  groupName?: string; // 编组名称
  teamId: string; // 班组ID
  teamName?: string; // 班组名称
  carriageNumber: number; // 车辆数
  taskTime?: string; // 截止时间
  whetherLinking: boolean; // 是否连挂
  orgId: string;
  stationId: string;
  isDefensiveTask?: boolean; // 是否防溜任务
  trackParkingOptions?: any[]; // 股道停车选项
}

// 分页查询参数
interface DefensiveTaskPageParams {
  currentPage: number;
  pageSize: number;
  orgId?: string;
  stationId?: string;
  trainNumber?: string; // 车次查询条件
}

// 防溜记录分页查询参数
interface DefensiveRecordPageParams {
  currentPage: number;
  pageSize: number;
  orgId?: string;
  stationId?: string;
  taskId?: string; // 作业ID
}

// 分页返回结果
interface PageResult<T> {
  records: T[];
  total: number;
  currentPage: number;
  pageSize: number;
}

const baseApi = '/web/jobDefensive';

/**
 * 分页查询防溜作业信息
 */
export function getDefensiveTaskPage(params: DefensiveTaskPageParams) {
  return Alova.Post<InResult<PageResult<DefensiveTaskInfo>>>(baseApi + '/page', { params });
}

/**
 * 分页查询防溜记录
 */
export function getDefensiveRecordPage(params: DefensiveRecordPageParams) {
  return Alova.Post<InResult<PageResult<DefensiveTaskDetail>>>(baseApi + '/record/page', {
    params,
  });
}

/**
 * 添加防溜作业
 */
export function addDefensiveTask(params: Partial<DefensiveTaskInfo>) {
  return Alova.Post<InResult<DefensiveTaskInfo>>(baseApi + '/add', { params });
}

/**
 * 更新防溜作业
 */
export function updateDefensiveTask(params: DefensiveTaskInfo) {
  return Alova.Post<InResult<DefensiveTaskInfo>>(baseApi + '/update', { params });
}

/**
 * 删除防溜作业
 */
export function removeDefensiveTask(params: { ids: string[] }) {
  return Alova.Post<InResult<boolean>>(baseApi + '/delete', { params });
}

/**
 * 提交防溜作业
 */
export function submitDefensiveTask(params: { id: string }) {
  return Alova.Post<InResult<boolean>>(baseApi + '/submit', { params });
}

/**
 * 获取防溜作业记录详情
 */
export function getDefensiveTaskRecord(params: { id: string }) {
  return Alova.Post<InResult<DefensiveTaskDetail>>(
    baseApi + '/record/detail',
    { params },
    {
      isShowMessage: false,
    }
  );
}

/**
 * 更新防溜作业记录
 */
export function updateDefensiveTaskRecord(params: DefensiveTaskDetail) {
  return Alova.Post<InResult<DefensiveTaskDetail>>(baseApi + '/record/update', { params });
}
